package cac.fenjian.resource.AoTask.MsAoMatchInfo.rest;

import cac.fenjian.resource.AoTask.MsAoMatchInfo.dao.MsAoMatchInfoDao;
import cac.fenjian.resource.AoTask.MsAoMatchInfo.dto.MsAoMatchInfoDTO;
import cac.fenjian.resource.AoTask.MsAoMatchInfo.service.MsAoMatchInfoService;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.PageParameter;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/MsAoMachInfo")
public class MsAoMachInfoRest {

    @Autowired
    private MsAoMatchInfoService msAoMatchInfoService;
    @Autowired
    private MsAoMatchInfoDao msAoMatchInfoDao;

    //分拣预配套下表数据查询
    @PostMapping("/searchByPage")
    public ResponseMsg<QueryRespBean<HashMap>> searchByPage(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<HashMap>> responseMsg = new ResponseMsg<>();
        String page = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
        String DTO = JSON.toJSONString(map.get("searchParams"));
        HashMap searchParams = JSON.parseObject(DTO, HashMap.class);
        QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
        queryReqBean.setPageParameter(pageParameter);
        queryReqBean.setSearchParams(searchParams);
        QueryRespBean<HashMap> result = msAoMatchInfoService.searchByPage(queryReqBean);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    //分拣配套数据新增
    @PostMapping("/insertMsMatchInfo")
    public ResponseMsg<Integer> insertMsMatchInfo(@RequestBody Map map) throws ParseException {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        List<Map> maps = (List<Map>) map.get("map");
        responseMsg.setResponseBody(msAoMatchInfoService.insertMsAoMatchInfoBasicList(maps));
        return responseMsg;
    }

    //删除
    @PostMapping("/delete")
    public ResponseMsg<Integer> deleteById(@RequestBody Map map) {
        String msAoMatchInfoId = (String) map.get("msAoMatchInfoId");
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msAoMatchInfoService.deleteBasic(msAoMatchInfoId));
        return responseMsg;
    }

    //批量删除
    @PostMapping("/deletes")
    public ResponseMsg<Integer> deleteByIds(@RequestBody Map map) {
        String[] msAoMatchInfoIds = (String[]) map.get("ids");
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msAoMatchInfoService.deletes(msAoMatchInfoIds));
        return responseMsg;
    }

    //查询工作台
    @PostMapping("querySortConsole")
    public HashMap querySortConsole() {
        HashMap hashMap = new HashMap();
        List<HashMap> querySortConsole = msAoMatchInfoDao.querySortConsole();
        hashMap.put("retCode", "200");
        hashMap.put("retData", querySortConsole);
        return hashMap;
    }

    //查询工作台工作包
    @PostMapping("queryPackageByConsole")
    public HashMap queryPackageByConsole(@RequestBody HashMap map) {
        HashMap hashMap = new HashMap();
        HashMap pageParameter = (HashMap) map.get("pageParameter");
        HashMap searchParams = (HashMap) map.get("searchParams");
        Integer pageNum = (Integer) pageParameter.get("currentPage");
        Integer pageSize = (Integer) pageParameter.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<HashMap> queryPackage = msAoMatchInfoDao.queryPackageByConsole(searchParams);
        PageInfo pageInfo = new PageInfo(queryPackage);
        hashMap.put("retCode", "200");
        hashMap.put("retData", queryPackage);
        hashMap.put("totalCount", pageInfo.getTotal());
        hashMap.put("totalPage", pageInfo.getPages());
        return hashMap;
    }

    //查询工作台Ao
    @PostMapping("queryAoByConsole")
    public HashMap queryAoByConsole(@RequestBody HashMap map) {
        HashMap hashMap = new HashMap();
        HashMap pageParameter = (HashMap) map.get("pageParameter");
        HashMap searchParams = (HashMap) map.get("searchParams");
        Integer pageNum = (Integer) pageParameter.get("currentPage");
        Integer pageSize = (Integer) pageParameter.get("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<HashMap> queryAo = msAoMatchInfoDao.queryAoByConsole(searchParams);
        PageInfo pageInfo = new PageInfo(queryAo);
        hashMap.put("retCode", "200");
        hashMap.put("retData", queryAo);
        hashMap.put("totalCount", pageInfo.getTotal());
        hashMap.put("totalPage", pageInfo.getPages());
        return hashMap;
    }

    //查询配套物料清单列表
    @PostMapping("queryMatchInfoByConsole")
    public HashMap queryMatchInfoByConsole(@RequestBody HashMap map) {
        HashMap hashMap = new HashMap();
        List<HashMap> queryMatchInfo = msAoMatchInfoDao.queryMatchInfoByConsole(map);
        hashMap.put("retCode", "200");
        hashMap.put("retData", queryMatchInfo);
        return hashMap;
    }

    //查询配套物料清单列表
    @PostMapping("queryMatchBillByConsole")
    public HashMap queryMatchBillByConsole(@RequestBody HashMap map) {
        HashMap hashMap = new HashMap();
        List<HashMap> queryMatchBill = msAoMatchInfoDao.queryMatchBillByConsole(map);
        hashMap.put("retCode", "200");
        hashMap.put("retData", queryMatchBill);
        return hashMap;
    }

    @PostMapping("selectMaterial")
    public PageInfo selectMaterial(@RequestBody HashMap param) {
        HashMap queryParam = (HashMap) param.get("queryParam");
        HashMap page = (HashMap) param.get("page");
        int startPage = Integer.parseInt(page.get("currentPage").toString());
        int pageSize = Integer.parseInt(page.get("pageSize").toString());
        return msAoMatchInfoService.selectMaterial(startPage, pageSize, queryParam);
    }
}
